package com.eims.sls_app.ui;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.ImageManager;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v13.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import bls.juejin.cn.R;

public class FirstActivity extends MyBaseActivity {

	private LinearLayout lr_main;
	private LinearLayout lr_main_1;
	private ImageView iv_start;
	private Bitmap bitmap;

	// 声明一个数组，用来存储所有需要动态申请的权限
	String[] permissions = new String[]{
			Manifest.permission.ACCESS_COARSE_LOCATION,
			Manifest.permission.ACCESS_FINE_LOCATION,
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.WRITE_SETTINGS};
	// 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
	List<String> mPermissionList = new ArrayList<>();


	/*
	 * 尺寸标准 hdpi:4.0 寸 480-800 、 xdpi:4.5寸720-1280、xxdpi:5.3寸1080-1980
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
			// 结束你的activity
			finish();
			return;
		}
		setContentView(R.layout.activity_splash);


//		ActionBar actionBar = getSupportActionBar();
//		actionBar.hide();





//		checkPermission();
		SharedPreferences sharedPreferences = getSharedPreferences("infonum", Activity.MODE_PRIVATE);
		// 使用getString方法获得value，注意第2个参数是value的默认值

		App.getInfoNum  = sharedPreferences.getString("num", "0");
	Log.e("sssss", App.getInfoNum );
		lr_main = (LinearLayout) this.findViewById(R.id.main);
		lr_main_1 = (LinearLayout) this.findViewById(R.id.main_1);
		iv_start = (ImageView) findViewById(R.id.iv_item_head);

//		iv_start.setImageBitmap(AppTools.getHttpBitmap("http://hqqd.ucaipiao.com/Images/appstart.png"));
//		ImageManager.getInstance().displayImage("http://hqqd.ucaipiao.com/Images/appstart.png", iv_start,
//				ImageManager.getViewsNOHuancunHeadOptions());
		SharedPreferences myAhare =getSharedPreferences("pinglun", Activity.MODE_PRIVATE);
		// 实例化SharedPreferences.Editor对象（第二步）
		SharedPreferences.Editor editor = myAhare.edit();
		// 用putString的方法保存数据
		editor.putInt("isfr", 0);
		
		editor.commit();
		getImgBg();

	}
	public void getImgBg(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				// 写子线程中的操作
//				bitmap=AppTools.getHttpBitmap("http://hqqd.ucaipiao.com/Images/appstart.png");
				handler.sendEmptyMessageDelayed(100,200);


			}
		}).start();
	}
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 100:
//				lr_main.setVisibility(View.GONE);
//				lr_main_1.setVisibility(View.VISIBLE);
//				iv_start.setImageBitmap(bitmap);
				  //图片资源
		      
				handler.sendEmptyMessageDelayed(200, 2000);
				break;
			case 200:
				SharedPreferences sharedPreferences = getSharedPreferences("app_user", Activity.MODE_PRIVATE);
				// 使用getString方法获得value，注意第2个参数是value的默认值

				boolean islogin  = sharedPreferences.getBoolean("isLogin", false);
				if (!islogin) {
					Intent intent = new Intent(FirstActivity.this, NewLoginActivity.class);
					startActivity(intent);
					finish();
				} else {
					Intent intent = new Intent(FirstActivity.this, MainActivity.class);
					startActivity(intent);
					finish();

				}

				break;
			default:
				break;
			}
		};
	};
	private void checkPermission() {

//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			mPermissionList.clear();
			/**
			 * 判断哪些权限未授予
			 * 以便必要的时候重新申请
			 */
			for (String permission : permissions) {

				Log.e("---", "111");
				if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
					Log.e("---", "222");
					ActivityCompat.requestPermissions(FirstActivity.this,new String []{permission},1);
					mPermissionList.add(permission);
				}
			}


//		}
	}


//	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//		switch (requestCode) {
//			case 1:
//				for (int i = 0; i < grantResults.length; i++) {
//					if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
//						//判断是否勾选禁止后不再询问
//						boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(FirstActivity.this, permissions[i]);
//						if (showRequestPermission) {
//							// 后续操作...
//						} else {
//							// 后续操作...
//						}
//					}
//				}
//				// 授权结束后的后续操作...
//				break;
//			default:
//				break;
//		}
//	}


}